package com.demo.service.credit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

// 授信服务工厂
@Component
public class CreditServiceFactory {

    private final Map<String, CreditService> creditServiceMap = new HashMap<>();

    @Autowired
    private List<CreditService> creditServices;

    @PostConstruct
    public void init() {
        for (CreditService service : creditServices) {
            creditServiceMap.put(service.getProductCode(), service);
//            creditServiceMap = creditServices.stream().collect(Collectors.toMap(CreditService::getProductCode, Function.identity()));
        }
    }

    // 根据产品码获取对应的授信服务
    public CreditService getCreditService(String productCode) {
        return creditServiceMap.get(productCode);
    }
}